package algorithm;

import java.util.Arrays;

/**
 * 283. 移动零
 * @author Panbo Guo
 * @date 2021/7/10
 */
public class T00283 {

    public static void main(String[] args) {
        int[] nums = {1,3,12,0,0};
        new Solution().moveZeroes(nums);
        System.out.println(Arrays.toString(nums));
    }

    static class Solution {
        public void moveZeroes(int[] nums) {
            if (nums == null || nums.length <= 1) {
                return;
            }
            for (int i = 0, j = 0; ;) {
                if (nums[i] == 0) {
                    if (j <= i) {
                        j = i + 1;
                    }
                    while (j < nums.length && nums[j] == 0) {
                        j ++;
                    }
                    if (j >= nums.length) {
                        break;
                    }
                    nums[i] = nums[j];
                    nums[j] = 0;
                }
                i ++;
                if (i >= nums.length) {
                    break;
                }
            }
        }
    }
}
